const { DataTypes } = require('sequelize')
const { sequelize } = require('../config/database')

const Transaction = sequelize.define('Transaction', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  userId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '用户ID'
  },
  amount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '交易金额(元)'
  },
  type: {
    type: DataTypes.ENUM('material_purchase', 'other'),
    allowNull: false,
    comment: '交易类型'
  },
  description: {
    type: DataTypes.STRING(255),
    allowNull: false,
    comment: '交易描述'
  },
  materialId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '相关素材ID（如果是购买素材）'
  },
  status: {
    type: DataTypes.ENUM('completed', 'failed', 'refunded'),
    defaultValue: 'completed',
    allowNull: false,
    comment: '交易状态'
  },
  extra: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '额外数据'
  }
}, {
  tableName: 'transactions',
  indexes: [
    {
      fields: ['userId']
    },
    {
      fields: ['type']
    },
    {
      fields: ['materialId']
    },
    {
      fields: ['status']
    }
  ]
})

module.exports = Transaction